package com.nd.zpark.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 	�ַ���������
 * @author hmx
 *
 */
public class StringUtils {


	
	/**
	 * �ж�һ���ַ����ǿջ���Ϊ���ַ���
	 * @param str
	 * @return
	 */
	@SuppressWarnings("unused")
	public static boolean isStrNull(String str) {
		
		if(str == null)
			return true;
		else if("".equals(str.trim()))
			return true;
		return false;
	}
	
	/**
	 * ��ȡ��ǰʱ��
	 * @param str ʱ���ʽ�ַ���
	 * @return
	 */
	public static String getStringDate(String str) {
		Date date = new Date();
		String dateString = "";
		try {
			SimpleDateFormat formatter = new SimpleDateFormat(str);
			dateString = formatter.format(date);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			return dateString;
		}
		
	}
	/**
	 * ��ȡ yyyy-MM-dd HH:mm:ss ��ǰʱ��
	 * @return
	 */
	public static String getYMDDate() {
		Date date = new Date();
		String dateString = "";
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			dateString = formatter.format(date);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			return dateString;
		}

	}


	/**
	 * ��ȡ��ǰʱ��
	 * @param format ʱ���ʽ�ַ���
	 * @return
	 * @throws ParseException 
	 */
	public static Date getDate(String format) throws ParseException {
		String stringDate = getStringDate("yyyy-MM-dd HH:mm:ss");
		 return getDate(stringDate, format);
	}
	
	/**
	 * �����ַ�����ʽת���ڸ�ʽ
	 * @param strDate
	 * @return
	 */
	public static Date getDate(String strDate,String format) throws ParseException{
		if(isStrNull(strDate)){
			return null;
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = null;
		date = simpleDateFormat.parse(strDate);
		System.out.println(date);
		return date;
	}
	/**
	 * 	��ȡΨһ��UUID
	 * @param 
	 * @return
	 */
	public static String getUUID() {
		String uuid = UUID.randomUUID().toString().replace("-", "");
		return uuid;
	}


	/**
	 * ����������һ����֮�󣬰���31�����
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public  static String subMonth(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dt = sdf.parse(date);
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(dt);
		rightNow.add(Calendar.MONTH, 1);
		Date dt1 = rightNow.getTime();
		String reStr = sdf.format(dt1);
		return reStr;
	}
	/**
	 * ��ȡ��������
	 * @return
	 * @throws ParseException
	 */
	public static String getTomorrowDay(String date,int i) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date sDate = sdf.parse(date);
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(sDate);
		calendar.add(Calendar.DAY_OF_MONTH, 1 * i);
		String yesterdayDate = dateFormat.format(calendar.getTime());
		return yesterdayDate;
	}


	public static void main(String[] args) throws ParseException {
		System.out.println(subMonth(getStringDate("yyyy-MM-dd HH:mm:ss")));
	}

	public static String bZero(int i) {
		// TODO Auto-generated method stub
		if(i < 10){
            return "0" + i;
        }
        return i+"";
	}

}